package org.diffshop.product.controller;

import org.diffshop.common.utils.R;
import org.diffshop.product.entity.CategoryEntity;
import org.diffshop.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;



/**
 * 商品三级分类
 *
 * @author tjf
 * @date 2024-05-13 10:35:49
 */
@RestController
@RequestMapping("product/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    /**
     * 查询商品分类的树状结构
     */
    @GetMapping("/list/tree")
    public R list(){
        List<CategoryEntity> result = categoryService.queryCateGoryListWithTree();

        return R.ok().put("data", result);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{catId}")
    public R info(@PathVariable("catId") Long catId){
		CategoryEntity category = categoryService.getById(catId);

        return R.ok().put("category", category);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody CategoryEntity category){
		categoryService.save(category);

        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public R update(@RequestBody CategoryEntity category){
		categoryService.updateDetail(category);

        return R.ok();
    }
    @PostMapping("/update/batch")
    public R updateBatch(@RequestBody CategoryEntity[] category){
        categoryService.updateBatchById(Arrays.asList(category));
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public R delete(@RequestBody Long[] catIds){
        // TODO 校验数组元素不能为0
		categoryService.removeCategoryByIds(Arrays.asList(catIds));

        return R.ok();
    }
}
